package cn.kinggm520.service.impl;

import cn.kinggm520.domain.Goods;
import cn.kinggm520.mapper.GoodsMapper;
import cn.kinggm520.mqConfig.RabbitMQConfig;
import cn.kinggm520.service.GoodsService;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 作者: kinggm Email:731586355@qq.com
 * 时间:  2020-05-22 11:58
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;


    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public void addGoods(Goods goods) {
        goodsMapper.addGoods(goods);

//        发送消息到MQ

        String spec = goods.getSpec();
        Map map = JSON.parseObject(spec, Map.class);
        goods.setSpecMap(map);

        rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE1, "goods.save",JSON.toJSON(goods).toString());


        System.out.println(goods.toString());

    }
}
